package com.csii.adhoc.system.auth.serviceImpl;

import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.info.user.UserPo;
//import com.csii.adhoc.info.user.service.UserService;
import com.csii.adhoc.system.auth.AuthPo;
import com.csii.adhoc.system.auth.TMgnDirAuthVo;
import com.csii.adhoc.system.auth.mapper.AuthMapper;
import com.csii.adhoc.system.auth.service.AuthService;
import com.csii.adhoc.util.DateUtils;
import com.csii.adhoc.util.ParamCheckUtils;
import com.csii.adhoc.util.TreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description: 权限接口实现类
 * @author: panChuanJiang
 * @create: 2021-01-28 15:20
 */
@Service("authService")
@Slf4j
//@CacheConfig(cacheNames = "user_interface")
public class AuthServiceImpl implements AuthService {
    @Resource
    private AuthMapper authMapper;
    @Resource
    private UidGenerator uidGenerator;
    //@Resource
   // private UserService userService;

    @Override
   // @CacheEvict(allEntries = true)
    @Transactional
    public void authMaintain(AuthPo authPo, String optMode) {
        if (StringUtils.isEmpty(authPo.getAuthId())) {
            insertOneAuth(authPo);
        } else {
            updateAuthByPrimaryKey(authPo);
        }
    }

    @Transactional
    public void updateAuthByPrimaryKey(AuthPo authPo) {
        authMapper.updateAuthByKey(authPo);
    }

    @Transactional
    public void deleteAuthByPrimaryKey(String authId) {
        authMapper.deleteAuthByPrimaryKey(authId);
    }

    @Transactional
    public void insertOneAuth(AuthPo authPo) {
        AuthPo po = authMapper.selectOneAuth(authPo);//查询确认是否有该权限记录
        if (po != null) {
            authPo.setAuthId(po.getAuthId());
            updateAuthByPrimaryKey(authPo);
        }else {
            String nowTime = DateUtils.getDateString("yyyyMMddHHmmss");
            authPo.setAuthId(uidGenerator.getUID() + "")
                    .setCreatedDt(nowTime)
                    .setUpdatedDt(nowTime)
                    .setCreatedBy(authPo.getUpdatedBy());
            authMapper.insertOneAuth(authPo);
        }
    }

    @Override
    //@Cacheable(key = "#name+'-'+#type")
    public List<TMgnDirAuthVo> getUserAuthority(String name, String type) {
        /*UserPo userPo = userService.selectUser(userId);
        String role = userPo.getRole();
        if (StringUtils.isEmpty(role)) {
            return null;
        }*/
        List<TMgnDirAuthVo> list = authMapper.getUserAuthority(name, type);
        return TreeUtils.createTree(list, "menuId");
    }

    @Override
    //@Cacheable(key = "#name")
    public Set<String> getUserInterface(String name) {
        List<TMgnDirAuthVo> list = authMapper.getUserAuthority(name,"3");
        HashSet<String> set = new HashSet<>();
        list.stream().filter(x -> !StringUtils.isEmpty(x.getMenuInterface()))
                .forEach(x -> set.addAll(Arrays.asList(x.getMenuInterface().split(","))));
        return set;
    }

    @Override
    public List<TMgnDirAuthVo> getRoleAuthority(String roleId, String type) {
        if (ParamCheckUtils.checkStringEmpty(roleId, type)) {
            throw new ParamValidityException(String.format("参数为空,roleId:%s ; type:%s", roleId, type));
        }
        List<TMgnDirAuthVo> list = authMapper.getRoleAuthority(roleId, type);
        list.forEach(x -> {
            x.setRoleid(roleId)
                    .setAuthEntid(x.getMenuId())
                    .setAuthCtrl(x.getAuthCtrl() == null ? "1" : x.getAuthCtrl());
        });
        return TreeUtils.createTree(list, "menuId");
    }

    @Override
    public List<AuthPo> selectListAuth(AuthPo authPo) {
        return authMapper.selectListAuth(authPo);
    }
}
